//
//  CYMainViewController.swift
//  CYWeiBo
//
//  Created by Mac on 16/7/9.
//  Copyright © 2016年 Mac. All rights reserved.
//

import UIKit

class CYMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //  越早执行越好，一般会放在appDelegate 渲染模式
//        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        //  把系统tabbar设置为指定tabbar
        let cyTabbar = CYTabBar()
        //  4.设置代理对象
        cyTabbar.cyDelegate = self
        
        //  传入的闭包
        cyTabbar.composeButtonClosure = { [weak self] in
            print("我是闭包调用过来的")
        }
        
        //  使用kvc给私有属性设置值
        setValue(cyTabbar, forKey: "tabBar")
        
        addChildViewController(CYHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(CYMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(CYDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(CYProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
    }
    //  重载addChildViewController
    func addChildViewController(childController: UIViewController, title: String, imageName: String) {
        
        //  设置tabBar的文字和显示图片
        //childController.tabBarItem.title = title
        
        //  导航栏上面的文字和tabbard 文字合并
        childController.title = title
        
        //  图片
        childController.tabBarItem.image = UIImage(named: imageName)
        
        //  图片高亮
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
        
        //  设置图片 的位置
        //childController.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: 0, bottom: -5, right: 0)
        
        //  指定渲染颜色
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //  设置tabbar字体颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        
        
        //  修改字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(14)], forState: .Normal)
        
        //  添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
        
    }



}

//  5.使用代理对象实现这个协议
extension CYMainViewController: CYTabBarDelegate {
    func didSelectedComposeButton() {
        print("我是代理对象调用过来的")
    }
    
}
